package com.jd.jdoss.service;

import com.jd.jdoss.config.RouteConfig;
import com.jd.jdoss.domain.utils.FileUpload;
import com.jd.jdoss.domain.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDate;
import java.util.UUID;

@Slf4j
@Service
public class FileService {

    @Autowired
    private RouteConfig routeConfig;

    public String saveFile(MultipartFile file,String filePath) {
        //扩展名格式
        String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String name ;
        /**
         * 指定上传目录
         */
        if(filePath != null && !"".equals(filePath.trim())) {
            name = designatedUp(filePath,file);
            return name;
        }
        /**
         * 默认上传目录
         */
        //图片类型文件
        if(this.inArray(routeConfig.getImageType(),extName)){
            filePath = routeConfig.getImageFolder();
        }
        //视频类型文件
       else if(this.inArray(routeConfig.getVideoType(),extName)){
            filePath = routeConfig.getVideoFolder();
        }
        //文档类型文件
        else if(this.inArray(routeConfig.getDocumentType(),extName)){
            filePath = routeConfig.getDocumentFolder();
        }
        //音频类型文件
       else if(this.inArray(routeConfig.getMusicType(),extName)){
            filePath = routeConfig.getMusicFolder();
        }else {
           return "This upload type is not supported temporarily";
        }
        name = myfileUp(filePath,file);
        return name;
    }

    /**
     * 删除文件
     * @param filePath 包含文件路径的文件名
     * @return
     */
    public String dropFile(String filePath) {
        try {
            FileUtil.delFile(routeConfig.getHardDisk()+File.separator+filePath);
            return "successful operation";
        } catch (Exception e) {
            return "drop file error";
        }
    }

    /**
     * 判断数组中是否包含某个元素
     * @param array 类型的数组
     * @param element 被检查的类型
     * @return
     */
    private boolean inArray(String[] array,String element) {
        boolean flag = false;
        for(String type : array) {
            if(element.equals(type)){
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 默认上传文件到文件夹
     * @param folder 默认文件夹
     * @param file 上传的文件
     * @return
     */
    private String myfileUp(String folder,MultipartFile file) {
        LocalDate today = LocalDate.now();
        String saveName = File.separator+today.getYear()+"."+today.getMonthValue()+ File.separator;
        String fileCode = UUID.randomUUID().toString().trim().replaceAll("-", "");
        String returnName= FileUpload.fileUp(file,routeConfig.getHardDisk()+File.separator+folder+saveName, fileCode);
        saveName = folder + File.separator +saveName + File.separator +returnName;
        log.warn("This file has been uploaded: "+saveName);
        return saveName;
    }

    /**
     * 指定目录上传文件
     * @param folder 指定文件夹
     * @param file 上传文件
     * @return
     */
    private String designatedUp(String folder,MultipartFile file) {
        String fileCode = UUID.randomUUID().toString().trim().replaceAll("-", "");
        String returnName= FileUpload.fileUp(file,routeConfig.getHardDisk()+File.separator+folder, fileCode);
        String filePathname = folder+File.separator+File.separator+returnName;
        log.warn("This file has been uploaded: "+filePathname);
        return filePathname;
    }

}
